﻿using Dapper;
using MySql.Data.MySqlClient;
using Net1.My.Web.Models;
using Net1.My.Web.Models.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net1.My.Web.DAL
{
    public class DAL_classes
    {
        private MySqlConnection mySqlConnection = new MySqlConnection(ConfigHelper.Configuration["MySQLConnectionString1"]);
        /// <summary>
        /// 返回对应id的一条学生数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public classes single(int id)
        {
            var single = mySqlConnection.QueryFirstOrDefault<classes>("select  * from classes where id=@id", new { id });
            return single;
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public List<classes> getClasses()
        {
            var list = mySqlConnection.Query<classes>("select  * from classes ").ToList();
            return list;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int insertClasses(classes s)
        {
            var sql = "insert into classes (class_name,major_name) values (@class_name,@major_name)";
            var affectedRows = mySqlConnection.Execute(sql, s);
            return affectedRows;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int editClasses(classes s)
        {
            var sql = "update classes set class_name=@class_name,major_name=@major_name where id = @id;";
            var ar = mySqlConnection.Execute(sql, s);
            return ar;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delClasses(int id)
        {
            var sql = "delete from classes where id = @id;";
            var affectedRows = mySqlConnection.Execute(sql, new { id });
            return affectedRows;
        }
        public List<classes> getco(string likestr)
        {
            var list = mySqlConnection.Query<classes>($"select * from classes where class_name like '%{likestr}%'or  major_name like '%{likestr}%'").ToList();
            return list;
        }
    }
}
